package org.sippr.service.impl;

import org.sippr.mapper.*;
import org.sippr.pojo.*;
import org.sippr.service.TaskQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskQueueImpl implements TaskQueueService {
    @Autowired
    private TaskQueueMapper taskQueueMapper;
    @Autowired
    private CreationMapper creationMapper;
    @Autowired
    private CreationStyleMapper creationStyleMapper;
    @Autowired
    private CreationWordCueMapper creationWordCueMapper;
    @Autowired
    private CreationWordNegativeMapper creationWordNegativeMapper;
    @Autowired
    private SdCheckPointsMapper sdCheckPointsMapper;

    //0代表等待中，1代表正在处理，2代表处理完成
    @Override
    public List<TaskQueue> findWatingTask() {

        List<TaskQueue> taskQueueList = taskQueueMapper.findTaskByState((short) 0);
        List<TaskQueue> res = new ArrayList<>();
        for (TaskQueue task : taskQueueList) {
            Integer creationId = task.getCreationId();
            Creation creation = creationMapper.selectByPrimaryKey(creationId);
            if (creation == null)//这个任务，有问题，忽略
            {
                System.out.println("任务" + task.getId() + "检测到Creation为空");
                continue;
            }

            task.setCreation(creation);

            //通过队列的creationId，获取任务的详细信息进行执行
            CreationStyle creationStyle = creationStyleMapper.findByCreationId(creationId);
            if (creationStyle == null)//这个任务，有问题，忽略
            {
                System.out.println("任务" + task.getId() + "检测到CreationStyle为空");
                continue;
            }
            task.setCreationStyle(creationStyle);

            SdCheckPoints sdCheckPoints = sdCheckPointsMapper.selectByPrimaryKey(creationStyle.getCptId());
            task.setSdCheckPoints(sdCheckPoints);

            CreationWordCue creationWordCue = creationWordCueMapper.findByCreationId(creationId);
            if (creationWordCue == null)//这个任务，有问题，忽略
            {
                System.out.println("任务" + task.getId() + "检测到CreationWordCue为空");
                continue;
            }
            task.setCreationWordCue(creationWordCue);

            CreationWordNegative creationWordNegative = creationWordNegativeMapper.findByCreationId(creationId);
            if (creationWordNegative == null)//这个任务，有问题，忽略
            {
                System.out.println("任务" + task.getId() + "检测到CreationWordNegative为空");
                continue;
            }
            task.setCreationWordNegative(creationWordNegative);

            res.add(task);
        }
        return res;
    }

    @Override
    public void setStateFinish(Integer id) {
        TaskQueue taskQueue = new TaskQueue();
        taskQueue.setId(id);
        taskQueue.setState((short) 2);
        taskQueueMapper.updateByPrimaryKeySelective(taskQueue);
    }

    @Override

    public void setStateWorkiing(Integer id) {
        TaskQueue taskQueue = new TaskQueue();
        taskQueue.setId(id);
        taskQueue.setState((short) 1);
        taskQueueMapper.updateByPrimaryKeySelective(taskQueue);
    }

    @Override
    public void setStateWaiting(Integer id) {
        TaskQueue taskQueue = new TaskQueue();
        taskQueue.setId(id);
        taskQueue.setState((short) 0);
        taskQueueMapper.updateByPrimaryKeySelective(taskQueue);
    }
}
